﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ShortcutCommands.cs" company="TechBits">
//   Copyright (c) TechBits. All rights reserved.
// </copyright>
// <author>
//   Leandro G Vacirca.
// </author>
// <summary>
//   Defines the ShortcutCommands type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace IntelliShortCut.Data
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    /// Defines the commands that are available for the user in the input commands prompt.
    /// </summary>
    public class ShortcutCommands
    {
        #region "Constants"

        /// <summary>
        /// Exit command keyword.
        /// </summary>
        private const string EXIT_KEYWORD = ".exit";

        /// <summary>
        /// All command keyword.
        /// </summary>
        private const string ALL_KEYWORD = "-a";

        /// <summary>
        /// Browser command keyword.
        /// </summary>
        private const string BROWSER_KEYWORD = "-b";

        /// <summary>
        /// Files command keyword.
        /// </summary>
        private const string FILES_KEYWORD = "-f";

        /// <summary>
        /// Folders command keyword.
        /// </summary>
        private const string FOLDERS_KEYWORD = "-d";

        /// <summary>
        /// Special folders command keyword.
        /// </summary>
        private const string SPECIAL_KEYWORD = "-s";

        /// <summary>
        /// Web command keyword.
        /// </summary>
        private const string WEB_KEYWORD = "-w";

        #endregion

        #region "Public Properties"

        /// <summary>
        /// Gets the Exit command keyword.
        /// </summary>
        public static string Exit
        {
            get
            {
                return EXIT_KEYWORD;
            }
        }

        /// <summary>
        /// Gets the All command keyword.
        /// </summary>
        public static string All
        {
            get
            {
                return ALL_KEYWORD;
            }
        }

        /// <summary>
        /// Gets Files command keyword.
        /// </summary>
        public static string Files
        {
            get
            {
                return FILES_KEYWORD;
            }
        }

        /// <summary>
        /// Gets Special.
        /// </summary>
        public static string Special
        {
            get
            {
                return SPECIAL_KEYWORD;
            }
        }

        /// <summary>
        /// Gets Folders command keyword.
        /// </summary>
        public static string Folders
        {
            get
            {
                return FOLDERS_KEYWORD;
            }
        }

        /// <summary>
        /// Gets the Browser command keyword.
        /// </summary>
        public static string Browser
        {
            get
            {
                return BROWSER_KEYWORD;
            }
        }

        /// <summary>
        /// Gets the Web command keyword.
        /// </summary>
        public static string Web
        {
            get
            {
                return WEB_KEYWORD;
            }
        }

        #endregion

        #region "Public Methods"

        /// <summary>
        /// Gets the autocompletion mode from a keyword.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <returns>
        /// The autocompletion mode. Null if the keyword does not represent an autocompletion mode.
        /// </returns>
        public static AutoCompletionModeEnum? GetModeFromKeyword(string keyword)
        {
            try
            {
                if (IsCommandKeyword(keyword))
                {
                    switch (keyword)
                    {
                        case ALL_KEYWORD:
                        {
                            return AutoCompletionModeEnum.All;
                        }

                        case FILES_KEYWORD:
                        {
                            return AutoCompletionModeEnum.Files;
                        }

                        case FOLDERS_KEYWORD:
                        {
                            return AutoCompletionModeEnum.Folders;
                        }

                        case SPECIAL_KEYWORD:
                        {
                            return AutoCompletionModeEnum.Special;                            
                        }

                        case BROWSER_KEYWORD:
                        {
                            return AutoCompletionModeEnum.Browser;
                        }

                        case WEB_KEYWORD:
                        {
                            return AutoCompletionModeEnum.Web;
                        }
                    }
                } 
                else if (keyword.StartsWith("."))
                {
                    // Removes the inicial dot
                    keyword = keyword.Substring(1).ToLower().Trim();
                    keyword = string.Concat(keyword[0].ToString().ToUpper(), keyword.Substring(1));

                    // Checks if the leyword is a valid mode
                    if (!GetAutoCompletionModes().Contains(keyword))
                    {
                        return null;
                    }

                    // Gets the autocompletion mode from the keyword
                    return (AutoCompletionModeEnum)Enum.Parse(typeof(AutoCompletionModeEnum), keyword);
                }
            }
            catch (Exception)
            {
                return null;
            }

            return null;
        }

        /// <summary>
        /// Verifies if the keyword is a command.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <returns>
        /// True: the keyword is a command; False: otherwise.
        /// </returns>
        public static bool IsCommandKeyword(string keyword)
        {
            return GetAll().Contains(keyword.ToLower());
        }

        #endregion

        #region "Private Methods"

        /// <summary>
        /// Returns all available commands.
        /// </summary>
        /// <returns>
        /// All available commands.
        /// </returns>
        private static List<string> GetAll()
        {
            return new List<string>
                       {
                           ALL_KEYWORD,
                           EXIT_KEYWORD,
                           FILES_KEYWORD,
                           FOLDERS_KEYWORD,
                           SPECIAL_KEYWORD,
                           BROWSER_KEYWORD,
                           WEB_KEYWORD
                       };
        }

        /// <summary>
        /// Gets the autocompletion mode names.
        /// </summary>
        /// <returns>
        /// The autocompletion mode names list.
        /// </returns>
        private static List<string> GetAutoCompletionModes()
        {
            return Enum.GetNames(typeof(AutoCompletionModeEnum)).ToList();
        }

        #endregion
    }
}
